/* $Id: core.js 2010-09-07 16:02 idris $ */

var like = {
		
	block: false,
	sender: {},
	url: {ajax:'', suggest:'', remove:''},
	clubs: {pages:{count:0, items:[]}, users:{count:0, items:[]}},
  loader_id: 'he_contacts_loading',
  disabled_div: 'he_disabled_div',
  $link: {},
  method: 'post',
  format: 'json',
  unlike_class: 'unlike',
  like_class: 'like',
  like_url: '',
  unlike_url: '',
	
	init: function(){
		var self = this;
		
		$$('.disable_like_club, .enable_like_club').addEvent('click', function(e){
			e.stop();
			self.url.ajax = this.href;
			self.sender = this;
			self.send();
		});
		
		if ($$('a.like_display_none')){
			$$('a.like_display_none').each(function($item){
				$item.getParent().setStyle('display', 'none');
			});
		}
		
		if ($$('.like_suggest')){
			$$('.like_suggest').addEvent('click', function(e){
				e.stop();
				self.url.suggest = this.href;
				he_contacts.box('like', 'getFriends', 'like.suggest', en4.core.language.translate('Suggest to Friends'), {'object':window.en4.core.subject.type, 'object_id':window.en4.core.subject.id}, 0);
			});
		}

    var guid = en4.core.subject.guid;
    if ($('_'+guid)){
      this.$link = $('_'+guid);
      if (this.$link.hasClass(this.unlike_class)){
        this.set_unlike();
      }else{
        this.set_like();
      }
    }
	},

  like: function(){
    var self = this;
    if (this.block){
      return ;
    }
    this.block = true;
    new Request.JSON/**/({
      method: self.method,
      url: self.like_url,
      data: {
        format: self.format
      },
      onSuccess: function(response){
        self.toggle();
        self.block = false;
        return true;
      }
    }).send();
  },

  unlike: function(){
    var self = this;
    if (this.block){
      return ;
    }
    this.block = true;
    new Request.JSON({
      method: self.method,
      url: self.unlike_url,
      data: {
        format: self.format
      },
      onSuccess: function(response){
        self.toggle();
        self.block = false;

        return true;
      }
    }).send();
  },

  toggle: function(){
    var $link = $(this.$link);
    if ($link.hasClass(this.unlike_class)){
      this.set_like();
    }else{
      this.set_unlike();
    }
  },

  set_like: function(){
    var $link = $(this.$link);
    var self = this;
    $link.getElements('span')[0].set('class', 'like_button');
    $link.getElements('span')[0].set('text', en4.core.language.translate('Like'));
    $link.set('class', 'like_button_link ' + self.like_class);    
    $link.removeEvents('click');
    $link.addEvent('click', function(){
      self.like();
    });
    this.$link = $($link);
    return this.$link;
  },

  set_unlike: function(){
    var $link = $(this.$link);
    var self = this;
    $link.removeEvents('click');
    $link.getElements('span')[0].set('class', 'unlike_button');
    $link.getElements('span')[0].set('text', en4.core.language.translate('Unlike'));
    $link.set('class', 'like_button_link ' + self.unlike_class);
    $link.addEvent('click', function(){
      self.unlike();
    });
    this.$link = $($link);
    return $link;
  },
	
	init_counts: function(){
		var self = this;
		
		if ($('like_users_count')){
			$('like_users_count').innerHTML = "(" + this.clubs.users.count + ")";
		}
		
		if ($('like_pages_count')){
			$('like_pages_count').innerHTML = "(" + this.clubs.pages.count + ")";
		}
		
		if ($$('.like_profile_pages')[0]){
			$$('.like_profile_pages')[0].addEvent('click', function(){
				self.make_active(this);
			});
		}
		
		if ($$('.like_profile_users')[0]){
			$$('.like_profile_users')[0].addEvent('click', function(){
				self.make_active(this);
			});
			this.make_active($$('.like_profile_users')[0]);
		}
		
		if ($$('.menu_like_profile')){
			$$('.menu_like_profile').addEvent('focus', function(){
				this.blur();
			});
		}
		
		if (!this.clubs.users.count){
			if ($$('.no_result_users')) $$('.no_result_users').removeClass('hidden');
		}
		
		if (!this.clubs.pages.count){
			if ($$('.no_result_pages')) $$('.no_result_pages').removeClass('hidden');
		}
	},
	
	make_active: function($link){
		if ($$('ul.like_navigation li.active')) $$('ul.like_navigation li.active').removeClass('active');
		
		$link.getParent().addClass('active');
	},
	
	list: function(type){
		var id = "like_club_"+type+"_container";
		
		if (!$(id).hasClass('hidden')){
			return ;
		}
		
		$$('.like_club_container').addClass('hidden');
		$(id).removeClass('hidden');
	},
	
	suggest: function(user_ids){
		var self = this;
		new Request.JSON({
			'url': self.url.suggest,
			'method': 'post',
			'data': {
				'user_ids': user_ids,
				'format': 'json'
			},
			onSuccess: function(){
				
			}
		}).send();
	},
	
	send: function(){
		if (this.block || !this.url.ajax){
			return;
		}
		
		this.block = true;
		var self = this;
		new Request.JSON({
			'url': self.url.ajax,
			'method': 'post',
			'data': {
				'format': 'json'
			},
			onSuccess: function(response){
				self.sender.getParent().innerHTML = response.html;
				self.init();
				self.block = false;
				
				switch (response.link){
					case 'disable': 
						if ($$('.like_promote')[0]) $$('.like_promote')[0].getParent().setStyle('display', 'block');
						if ($$('.like_send_update')[0]) $$('.like_send_update')[0].getParent().setStyle('display', 'block');
						if ($$('.like_suggest')[0]) $$('.like_suggest')[0].getParent().setStyle('display', 'block');
            if ($$('.like_button_container')[0]) $$('.like_button_container')[0].setStyle('display', 'block');
					break;
					case 'enable': 
						if ($$('.like_promote')[0]) $$('.like_promote')[0].getParent().setStyle('display', 'none');
						if ($$('.like_send_update')[0]) $$('.like_send_update')[0].getParent().setStyle('display', 'none');
						if ($$('.like_suggest')[0]) $$('.like_suggest')[0].getParent().setStyle('display', 'none');
            if ($$('.like_button_container')[0]) $$('.like_button_container')[0].setStyle('display', 'none');
					break;
				}
			}
		}).send();
	},
	
	see_all: function(object, object_id){
		he_list.box('like', 'getLikes', 'Likes', {'object':object, 'object_id':object_id});
	},

  see_all_liked: function(user_id){
    var $el = new Element('a', {'href': 'like/see-liked/user_id/'+user_id, 'class': 'smoothbox'});
    Smoothbox.open($el);
  },

  list_like: function(object, object_id, $element){
    var self = this;
    $element = $($element);
    if (this.block){
      return false;
    }
    this.block = true;
    new Request.JSON({
      'url':'like/like/object/'+object+'/object_id/'+object_id,
      'method':'post',
      'data':{
        'format':'json'
      },
      onSuccess: function(response){
        self.block = false;
        if (response.error == true){
          return false;
        }
        $('unlike_'+object+'_'+object_id).getParent().setStyle('display', 'block');
        $('like_'+object+'_'+object_id).getParent().setStyle('display', 'none');
      }
    }).send();
  },

  list_unlike: function(object, object_id, $element){
    var self = this;
    $element = $($element);
    if (this.block){
      return false;
    }
    this.block = true;
    new Request.JSON({
      'url':'like/unlike/object/'+object+'/object_id/'+object_id,
      'method':'post',
      'data':{
        'format':'json'
      },
      onSuccess: function(response){
        self.block = false;
        if (response.error == true){
          return false;
        }
        $('unlike_'+object+'_'+object_id).getParent().setStyle('display', 'none');
        $('like_'+object+'_'+object_id).getParent().setStyle('display', 'block');
      }
    }).send();
  },
  
  init_buttons: function(){
    var self = this;
    if ($$('div.like_button_container a.unlike')){
      var $btns = $$('.like_button_container a.unlike');
      $btns.removeEvents('click');     
      $btns.each(function($item){
        if ($item.hasClass('unlike')){
          $item.addEvent('click', function(){
            var id = this.id;
            var guid = id.substr(7);
            var info = guid.split('_');
            self.list_unlike(info[0], info[1], $item);
          });
        }
      });
    }

    if ($$('div.like_button_container a.like')){
      var $btns = $$('.like_button_container a.like');
      $btns.removeEvents('click');
      $btns.each(function($item){
        if ($item.hasClass('like')){
          $item.addEvent('click', function(){
            var id = this.id;
            var guid = id.substr(5);
            var info = guid.split('_');
            self.list_like(info[0], info[1], $item);
          });
        }
      });
    }
  },
  
  select_list: function(list, user_id, $link){
    var self = this;
    self.show_loader();
    new Request.JSON({
      'url': 'like/see-liked/list/'+list+'/user_id/'+user_id,
      'method': 'post',
      'data': {
        'format': 'json'
      },
      onSuccess: function(response){
        $('he_list').innerHTML = response.html;
        if ($$('.select_btns .active')){
          $$('.select_btns .active').each(function($element){
            $($element).removeClass('active');
          });
        }
        $($link).addClass('active');
        self.hide_loader();
        self.init_buttons();
      }
    }).send();
  },
	
	do_remove: function(object, object_id){
	  var self = this;
    var $item = $('like_'+object+'_'+object_id);
    $item.dispose();
    
    new Request.JSON({
      'url':self.url.remove,
      'method':'post',
      'data':{
        'object': object,
        'object_id':object_id,
        'format':'json'
      },
      onSuccess: function(response){
        self.counting(object+'s', -1);
      }
    }).send();
	},
	
	remove: function(object, object_id){
		var self = this;
		var callback = function(){self.do_remove(object, object_id)};
		he_show_confirm(en4.core.language.translate("Unlike"), en4.core.language.translate("Are you sure you want to unlike this?"), callback);
	},
	
	counting: function(type, count){
		var prev = $('like_'+type+'_count').innerHTML.substr(1).toInt();
		var next = prev + count;
		$('like_'+type+'_count').innerHTML = "("+next+")";
		if (!next){
			$$('.no_result_'+type).removeClass('hidden');
		}
	},
	
	send_update: function($form){
		var self = this;
		var subject = $('subject').value;
		var body = $('body').value;
		var url = $form.action;
		
		if (this.block){
			return false;
		}
		
		new Request.JSON({
			'url': url,
			'method': 'post',
			'data': {
				'subject': subject,
				'body': body,
				'format': 'json'
			},
			onSuccess: function(response){
				if (response.error == 0){
					$$('body')[0].innerHTML = response.message;
					setTimeout(function(){window.parent.Smoothbox.close()}, 1000);
				}else{
					$('send_update_errors').innerHTML = response.message;
					$('send_update_errors').removeClass('hidden');
					setTimeout(function(){$('send_update_errors').addClass('hidden')}, 1000);
				}
			}
		}).send();
		
		return false;
	},

  show_loader: function(){
    $(this.loader_id).removeClass('hidden');
    $(this.disabled_div).removeClass('hidden');
  },

  hide_loader: function(){
    $(this.loader_id).addClass('hidden');
  },

  toggle_menu: function($link){
    $link = $($link);
    if ($$('.like_profile_menu')[0].hasClass('hidden')){
      $$('.like_profile_menu')[0].removeClass('hidden');
      $link.set('text', '-');
      $link.set('title', en4.core.language.translate('Hide'));
    }else{
      $$('.like_profile_menu')[0].addClass('hidden');
      $link.set('text', '+');
      $link.set('title', en4.core.language.translate('Show Like'));
    }
  }
}